using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Globalization;

namespace TST.SharePoint.Shared
{
    public class WSSFieldCurrency : FieldCurrency, IDataFormatting
    {
        private SPFieldCurrency _field;

        /// <summary>
        /// Constructor.
        /// </summary>
        public WSSFieldCurrency()
        {
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="ID">The ID for the field.</param>
        public WSSFieldCurrency(string Id)
            : base(Id)
        {
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        public WSSFieldCurrency(SPFieldCurrency field)
        {
            this.Id = field.Id.ToString();
            _field = field;
        }

        public int CurrencyLocaleId
        {
            get
            {
                if (_field != null)
                    return _field.CurrencyLocaleId;
                return 0;
            }
        }

        public override object GetTypedValue(object value)
        {
            if (value == null || _field == null)
                return null;
            string stringValue = value.ToString();
            double result;
            if (double.TryParse(stringValue.ToString(), System.Globalization.NumberStyles.Any, new CultureInfo(1033), out result))
                return result;
            return null;
        }

        public override string GetTypedValueAsString(object value)
        {
            if (value == null || _field == null)
                return null;
            return ((double)value).ToString(new CultureInfo(1033));
        }

        public override string GetTypedValueFormatted(object value)
        {
           
            if (value == null || _field == null)
                return null;
            IDataFormatting formatting = this as IDataFormatting;
            if (formatting != null)
            {
                return ((double)value).ToString(formatting.DataFormatString, new CultureInfo(CurrencyLocaleId));
            }
            return ((double)value).ToString(new CultureInfo(1033));
        }

        #region IDataFormatting Members

        string IDataFormatting.DataFormatString
        {
            get
            {
                if (_field != null)
                    return GetFormatString(_field.DisplayFormat);
                return "{0:C}";
            }
        }

        public static string GetFormatString(SPNumberFormatTypes format)
        {
            switch (format)
            {
                case SPNumberFormatTypes.FiveDecimals:
                    return "C5";
                case SPNumberFormatTypes.FourDecimals:
                    return "C4";
                case SPNumberFormatTypes.NoDecimal:
                    return "C0";
                case SPNumberFormatTypes.OneDecimal:
                    return "C1";
                case SPNumberFormatTypes.ThreeDecimals:
                    return "C3";
                case SPNumberFormatTypes.TwoDecimals:
                    return "C2";
                default:
                    return "C";
            }
        }

        #endregion
    }
}
